package com.pg.auth.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pg.auth.model.dto.sysorg.SysOrgConvertor;
import com.pg.auth.model.dto.sysorg.SysOrgDTO;
import com.pg.auth.model.dto.sysorg.SysOrgReq;
import com.pg.auth.model.dto.sysorg.SysOrgVO;
import com.pg.auth.model.entity.SysOrg;
import com.pg.auth.model.mapper.SysOrgMapper;
import com.pg.auth.service.SysOrgService;
import com.pg.base.component.PageVO;
import com.pg.base.component.Pager;
import com.pg.base.enums.DeleteEnum;
import com.pg.base.vo.LoginVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/***
 * SysOrgServiceImpl
 * @author : huhuaping
 * @Date : 2024-06-06 20:59:34
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysOrgServiceImpl implements SysOrgService {

    private final SysOrgMapper sysOrgMapper;

    @Override
    public PageVO<SysOrgVO> page(Pager<SysOrgReq, SysOrgVO> pageReq){

        QueryWrapper<SysOrg> queryWrapper = baseQueryWrapper(pageReq.getParam());

        PageVO<SysOrgVO> result = sysOrgMapper.selectPage(pageReq.getPageVO(), queryWrapper);
        return result;
    }

    @Override
    public List<SysOrgVO> list(SysOrgReq sysOrgReq){

        QueryWrapper<SysOrg> queryWrapper = baseQueryWrapper(sysOrgReq);

        List<SysOrg> sysOrgList = sysOrgMapper.selectList(queryWrapper);

        return SysOrgConvertor.INSTANCE.listDbToVO(sysOrgList);

    }

    @Override
    public SysOrgVO getById(Long orgId) {
        SysOrg sysOrg = sysOrgMapper.selectById(orgId);
        return SysOrgConvertor.INSTANCE.dbToVO(sysOrg);
    }

    @Override
    public Long add(SysOrgDTO sysOrgDTO,LoginVO userVo) {
        SysOrg sysOrgAdd = SysOrgConvertor.INSTANCE.dtoToDB(sysOrgDTO);
        sysOrgMapper.insert(sysOrgAdd);
        return sysOrgAdd.getOrgId();
    }

    @Override
    public Integer updateById(SysOrgDTO sysOrgDTO,LoginVO userVo) {
        SysOrg sysOrgAdd = SysOrgConvertor.INSTANCE.dtoToDB(sysOrgDTO);
        int count = sysOrgMapper.updateById(sysOrgAdd);
        return count;
    }

    @Override
    public int deleteById(Long orgId) {
        SysOrg sysOrg = sysOrgMapper.selectById(orgId);

        // 封装对象
        SysOrg sysOrgDelete = new SysOrg();

        sysOrgDelete.setOrgId(orgId);
        sysOrgDelete.setDeleteStatus(DeleteEnum.DELETE.getValue());

        return sysOrgMapper.updateById(sysOrgDelete);

    }


    private QueryWrapper<SysOrg> baseQueryWrapper(SysOrgReq sysOrg) {
        QueryWrapper<SysOrg> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<SysOrg> lambda =  queryWrapper.lambda();
        lambda.eq(SysOrg::getDeleteStatus,DeleteEnum.NORMAL.getValue());
            lambda.orderByDesc(SysOrg::getOrgId);
        if (ObjectUtil.isNotEmpty(sysOrg.getOrgId())) {
            lambda.eq(SysOrg::getOrgId, sysOrg.getOrgId());
        }
        if (ObjectUtil.isNotEmpty(sysOrg.getOrgCode())) {
            lambda.eq(SysOrg::getOrgCode, sysOrg.getOrgCode());
        }
        if (ObjectUtil.isNotEmpty(sysOrg.getOrgShortName())) {
            lambda.eq(SysOrg::getOrgShortName, sysOrg.getOrgShortName());
        }
        if (ObjectUtil.isNotEmpty(sysOrg.getOrgName())) {
            lambda.eq(SysOrg::getOrgName, sysOrg.getOrgName());
        }
        if (ObjectUtil.isNotEmpty(sysOrg.getOrgStatus())) {
            lambda.eq(SysOrg::getOrgStatus, sysOrg.getOrgStatus());
        }
        if (ObjectUtil.isNotEmpty(sysOrg.getOrgLevel())) {
            lambda.eq(SysOrg::getOrgLevel, sysOrg.getOrgLevel());
        }
        if (ObjectUtil.isNotEmpty(sysOrg.getParentId())) {
            lambda.eq(SysOrg::getParentId, sysOrg.getParentId());
        }
        if (ObjectUtil.isNotEmpty(sysOrg.getOrgTree())) {
            lambda.eq(SysOrg::getOrgTree, sysOrg.getOrgTree());
        }
        if (ObjectUtil.isNotEmpty(sysOrg.getValidTime())) {
            lambda.eq(SysOrg::getValidTime, sysOrg.getValidTime());
        }
        if (ObjectUtil.isNotEmpty(sysOrg.getLoseTime())) {
            lambda.eq(SysOrg::getLoseTime, sysOrg.getLoseTime());
        }
        if (ObjectUtil.isNotEmpty(sysOrg.getDomain())) {
            lambda.eq(SysOrg::getDomain, sysOrg.getDomain());
        }
        if (ObjectUtil.isNotEmpty(sysOrg.getRemark())) {
            lambda.eq(SysOrg::getRemark, sysOrg.getRemark());
        }
        if (ObjectUtil.isNotEmpty(sysOrg.getCreateUser())) {
            lambda.eq(SysOrg::getCreateUser, sysOrg.getCreateUser());
        }
        if (ObjectUtil.isNotEmpty(sysOrg.getCreateUserName())) {
            lambda.eq(SysOrg::getCreateUserName, sysOrg.getCreateUserName());
        }
        if (ObjectUtil.isNotEmpty(sysOrg.getCreateTime())) {
            lambda.eq(SysOrg::getCreateTime, sysOrg.getCreateTime());
        }
        if (ObjectUtil.isNotEmpty(sysOrg.getUpdateUser())) {
            lambda.eq(SysOrg::getUpdateUser, sysOrg.getUpdateUser());
        }
        if (ObjectUtil.isNotEmpty(sysOrg.getUpdateUserName())) {
            lambda.eq(SysOrg::getUpdateUserName, sysOrg.getUpdateUserName());
        }
        if (ObjectUtil.isNotEmpty(sysOrg.getUpdateTime())) {
            lambda.eq(SysOrg::getUpdateTime, sysOrg.getUpdateTime());
        }
        if (ObjectUtil.isNotEmpty(sysOrg.getDeleteStatus())) {
            lambda.eq(SysOrg::getDeleteStatus, sysOrg.getDeleteStatus());
        }
        return queryWrapper;
    }

}
